package com.songshuai.pangu.api.component;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.songshuai.pangu.api.controller.BaseController;
import com.songshuai.webapi.baseapi.dto.pangu.request.service.AddInviteServiceItemReqDto;
import com.songshuai.webapi.baseapi.dto.pangu.request.service.ServiceItemReqDto;
import com.songshuai.webapi.baseapi.dto.pangu.request.service.ServiceReqDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;

/**
 * 对接盘古-服务流程标准化
 * @Author: lhx
 * @Date: 2019/4/10 19:44
 */
@Slf4j
@Component
public class ServiceItemComponent extends BaseController {



    //获取服务一体化内容
    @Value("${pangu.serviceitem.getInviteServiceItemTree}")
    private String getInviteServiceItemTreeUrl;


    //获取邀约服务已反馈信息
    @Value("${pangu.serviceitem.getInviteServiceItems}")
    private String getInviteServiceItemsUrl;


    //新增邀约服务反馈信息
    @Value("${pangu.serviceitem.addInviteServiceItem}")
    private String addInviteServiceItemUrl;



    /**
     *查询获取服务一体化内容
     * @return
     */
    public String getInviteServiceItemTree(){
        try {
            return getCallPanguApi(new HashMap(),getInviteServiceItemTreeUrl);
        } catch (Exception e) {
            log.error("请求盘古-获取服务一体化内容异常",e);
        }
        return null;
    }


    /**
     * 获取邀约服务已反馈信息
     * @param reqDto
     * @return
     */
    public String getInviteServiceItems(ServiceReqDto reqDto){
        try {
            JSONObject object = new JSONObject();
            object.put("OperateType", reqDto.getOperateType());
            object.put("InviteNo",reqDto.getInviteNo());
            object.put("OperateId",reqDto.getOperateId());
            return callPanguApi(object, getInviteServiceItemsUrl);
        } catch (Exception e) {
            log.error("请求盘古-获取邀约服务已反馈信息异常",e);
        }
        return null;
    }


    /**
     * 新增邀约服务反馈信息
     * @param reqDto
     * @return
     */
    public String addInviteServiceItem(AddInviteServiceItemReqDto reqDto){
        try {
            JSONObject object = new JSONObject();
            object.put("OperateType", reqDto.getOperateType());
            object.put("InviteNo",reqDto.getInviteNo());
            object.put("OperatorId",reqDto.getOperatorId());
            object.put("Remark",reqDto.getRemark());
            JSONArray array = new JSONArray();
            List<ServiceItemReqDto> items = reqDto.getItems();
            if (!CollectionUtils.isEmpty(items)){
                items.forEach(x->{
                    JSONObject item = new JSONObject();
                    item.put("ItemId",x.getItemId());
                    item.put("ParentId",x.getParentId());
                    array.add(item);
                });
            }
            object.put("Items",array);
            return callPanguApi(object, addInviteServiceItemUrl);
        } catch (Exception e) {
            log.error("请求盘古-新增邀约服务反馈信息异常",e);
        }
        return null;
    }








}
